Authored: Kathy N. Lam

Experiment: March 8, 2020

Updated: April 15, 2021

Set up

library(flowCore) #for reading and manipulating flow data
library(ggcyto) #for using ggplot with flow data
library(scales) #for nice plot axes
library(cowplot) #for multi panel plots
library(Phenoflow) #for rarefying
library(tidyverse) #for data wrangling and plotting

Read data

(fs = read.flowSet(path="data_fcs"))
## A flowSet with 100 experiments.
## 
##   column names:
##   530/30 Blue B-A 610/20 YG C-A FSC-A FSC-H FSC-W SSC-A SSC-H SSC-W Time
colnames(fs) 
## [1] "530/30 Blue B-A" "610/20 YG C-A"   "FSC-A"           "FSC-H"          
## [5] "FSC-W"           "SSC-A"           "SSC-H"           "SSC-W"          
## [9] "Time"
pData(phenoData(fs))
#read in sample names and merge
metadata = read_tsv("metadata.tsv") %>%
    rename(name=Filename) %>%
    mutate(Mouse = paste("Mouse", Mouse)) %>%
    mutate(Day = paste("Day", Timepoint)) %>%
    mutate(Treatment_Mouse = paste0(Treatment, "\n", Mouse)) %>%
    mutate(Mouse_Treatment = paste0(Mouse, " (", Treatment, ")"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   FlowSampleNumber = col_double(),
##   `Mouse Sample Number` = col_double(),
##   Experiment = col_character(),
##   Treatment = col_character(),
##   Timepoint = col_double(),
##   Mouse = col_double(),
##   Filename = col_character()
## )
metadata$Mouse = factor(metadata$Mouse, levels= unique(metadata$Mouse))
metadata$Treatment_Mouse = factor(metadata$Treatment_Mouse, levels=unique(metadata$Treatment_Mouse))
metadata$Mouse_Treatment = factor(metadata$Mouse_Treatment, levels=unique(metadata$Mouse_Treatment))
metadata$Treatment = factor(metadata$Treatment, levels=unique(metadata$Treatment))
metadata$Day = factor(metadata$Day, levels=unique(metadata$Day))
metadata
#add columns to phenoData  
phenoData(fs)$Order = seq(1, length(phenoData(fs)$name))
phenoData(fs)$Treatment = metadata$Treatment
phenoData(fs)$Timepoint = metadata$Timepoint
phenoData(fs)$Day = metadata$Day
phenoData(fs)$Name = metadata$name
phenoData(fs)$Mouse = metadata$Mouse
phenoData(fs)$Treatment_Mouse = metadata$Treatment_Mouse
phenoData(fs)$Mouse_Treatment = metadata$Mouse_Treatment
pData(phenoData(fs))
#make labeller function for facet_wrap
order = as.character(phenoData(fs)$Order)
name = phenoData(fs)$Name

order_names = mapply(c, order, name, SIMPLIFY = FALSE) #make a one-to-one 
order_names = lapply(order_names, `[[`, 2) #keep second element of each vector in the list
order_names = order_names[as.character(sort(as.numeric(names(order_names))))] #numerically sort 

order_labeller = function(variable,value){
  return(order_names[value])
}

Gate on scatter

scatter = rbind(c(0,   1e4), 
                c(1e5, 1e4),
                c(1e5, 2.5e5),  
                c(0,   2.5e5))
colnames(scatter)=c("FSC-A", "SSC-A")
scatter = as.data.frame(scatter)

ggplot() + 
    geom_point(data=fs, aes(x=`FSC-A`, y=`SSC-A`), shape=16, size=0.75, alpha=0.5) +
    scale_y_continuous(name="SSC-A (Granularity)\n", limits = c(-2e1,3e5)) +
    scale_x_continuous(name="\nFSC-A (Size)", limit=c(-2e1,3e5)) +
    facet_grid(Mouse~Timepoint) +
    theme_linedraw(14) +
    theme(panel.grid = element_blank(), axis.text.x=element_text(angle=90,hjust=1)) +
    geom_polygon(data=scatter, aes(x=`FSC-A`, y=`SSC-A`), fill=NA, colour="indianred", size=0.5,  linetype="solid") 
## Warning: Removed 5093378 rows containing missing values (geom_point).


gate_scatter = polygonGate(filterId="scatter", `FSC-A` = scatter$`FSC-A`, `SSC-A` = scatter$`SSC-A`) 
result = flowCore::filter(fs, gate_scatter)
events = flowCore::Subset(fs, result)

Rarefy

events = Phenoflow::FCS_resample(events, replace = FALSE, rarefy = FALSE, progress = TRUE)
## Your samples range between 30216 and 112724 cells
## Your samples were randomly subsampled to 30216 cells

Gate on fluorescence

red = rbind(
    c(-1e3,   4e3),
    c(-1e3,   4e5),
    c( 4000,  4e5),
    c( 1000,  4e3))
colnames(red)=c("530/30 Blue B-A", "610/20 YG C-A")
red = as.data.frame(red)

yellow = rbind(
    c(3e3,  3e3),
    c(9e2,  2e3),
    c(5e3,  4e5),
    c(1e5,  4e5),
    c(1e5,  4e4))
colnames(yellow)=c("530/30 Blue B-A", "610/20 YG C-A")
yellow = as.data.frame(yellow)

#check polygons 
ggplot() + 
    geom_polygon(data=yellow, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), fill=NA, colour="goldenrod4", size=0.5, linetype="solid") +
    geom_polygon(data=red, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), fill=NA, colour="red3", size=0.5, linetype="solid") +
    theme_linedraw(12) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1e3, 5.5e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1e3, 3e5)) +
    theme(panel.grid = element_blank(), 
          axis.title.x = element_text(colour="green4"), 
          axis.title.y = element_text(colour="red3")) 

ggplot() + 
    geom_polygon(data=yellow, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), fill=NA, colour="gold4", size=0.5, linetype="solid") +
    geom_polygon(data=red, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), fill=NA, colour="red3", size=0.5, linetype="solid") +
    geom_point(data=events, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2) +
    theme_linedraw(12) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 6e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 4e5)) +
    theme(panel.grid = element_blank(), 
          axis.title.x = element_text(colour="green4"), 
          axis.title.y = element_text(colour="red3")) +
    facet_grid(Mouse~Timepoint)  
## Warning: Removed 174 rows containing missing values (geom_point).

gate_yellow = polygonGate(filterId="yellow", `530/30 Blue B-A` = yellow$`530/30 Blue B-A`, `610/20 YG C-A` = yellow$`610/20 YG C-A`)
result_yellow = flowCore::filter(events, gate_yellow)
events_yellow = Subset(events, result_yellow)
gate_red = polygonGate(filterId="red", `530/30 Blue B-A` = red$`530/30 Blue B-A`, `610/20 YG C-A` = red$`610/20 YG C-A`)
result_red = flowCore::filter(events, gate_red)
events_red = Subset(events, result_red)
percent_red = toTable(summary(result_red)) %>% 
    mutate(x = 6e5, y = 0, colour="red", order=phenoData(events)$order) %>%
    rename(name=sample) %>%
    left_join(metadata, by="name")
## filter summary for frame 'Specimen_001_Sample_001_001.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_002_002.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_003_003.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_004_004.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_005_005.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_006_006.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_007_007.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_008_008.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_009_009.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_010_010.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_011_011.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_012_012.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_013_013.fcs'
##  red+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_014_014.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_015_015.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_016_016.fcs'
##  red+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_017_017.fcs'
##  red+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_018_018.fcs'
##  red+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_019_019.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_020_020.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_021_021.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_022_022.fcs'
##  red+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_023_023.fcs'
##  red+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_024_024.fcs'
##  red+: 11 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_025_025.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_026_026.fcs'
##  red+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_027_027.fcs'
##  red+: 6 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_028_028.fcs'
##  red+: 130 of 30216 events (0.43%)
## 
## filter summary for frame 'Specimen_001_Sample_029_029.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_030_030.fcs'
##  red+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_031_031.fcs'
##  red+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_032_032.fcs'
##  red+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_033_033.fcs'
##  red+: 11 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_034_034.fcs'
##  red+: 9 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_035_035.fcs'
##  red+: 11 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_036_036.fcs'
##  red+: 26 of 30216 events (0.09%)
## 
## filter summary for frame 'Specimen_001_Sample_037_037.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_038_038.fcs'
##  red+: 6 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_039_039.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_040_040.fcs'
##  red+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_041_041.fcs'
##  red+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_042_042.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_043_043.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_044_044.fcs'
##  red+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_045_045.fcs'
##  red+: 56 of 30216 events (0.19%)
## 
## filter summary for frame 'Specimen_001_Sample_046_046.fcs'
##  red+: 6 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_047_047.fcs'
##  red+: 12 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_048_048.fcs'
##  red+: 13 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_049_049.fcs'
##  red+: 39 of 30216 events (0.13%)
## 
## filter summary for frame 'Specimen_001_Sample_050_050.fcs'
##  red+: 16 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_051_051.fcs'
##  red+: 108 of 30216 events (0.36%)
## 
## filter summary for frame 'Specimen_001_Sample_052_052.fcs'
##  red+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_053_053.fcs'
##  red+: 3434 of 30216 events (11.36%)
## 
## filter summary for frame 'Specimen_001_Sample_054_054.fcs'
##  red+: 3688 of 30216 events (12.21%)
## 
## filter summary for frame 'Specimen_001_Sample_055_055.fcs'
##  red+: 15 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_056_056.fcs'
##  red+: 45 of 30216 events (0.15%)
## 
## filter summary for frame 'Specimen_001_Sample_057_057.fcs'
##  red+: 18 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_058_058.fcs'
##  red+: 3308 of 30216 events (10.95%)
## 
## filter summary for frame 'Specimen_001_Sample_059_059.fcs'
##  red+: 16 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_060_060.fcs'
##  red+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_061_061.fcs'
##  red+: 9 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_062_062.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_063_063.fcs'
##  red+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_064_064.fcs'
##  red+: 26 of 30216 events (0.09%)
## 
## filter summary for frame 'Specimen_001_Sample_065_065.fcs'
##  red+: 41 of 30216 events (0.14%)
## 
## filter summary for frame 'Specimen_001_Sample_066_066.fcs'
##  red+: 9 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_067_067.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_068_068.fcs'
##  red+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_069_069.fcs'
##  red+: 13 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_070_070.fcs'
##  red+: 98 of 30216 events (0.32%)
## 
## filter summary for frame 'Specimen_001_Sample_071_071.fcs'
##  red+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_072_072.fcs'
##  red+: 7 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_073_073.fcs'
##  red+: 8248 of 30216 events (27.30%)
## 
## filter summary for frame 'Specimen_001_Sample_074_074.fcs'
##  red+: 2409 of 30216 events (7.97%)
## 
## filter summary for frame 'Specimen_001_Sample_075_075.fcs'
##  red+: 19 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_076_076.fcs'
##  red+: 41 of 30216 events (0.14%)
## 
## filter summary for frame 'Specimen_001_Sample_077_077.fcs'
##  red+: 7 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_078_078.fcs'
##  red+: 9598 of 30216 events (31.76%)
## 
## filter summary for frame 'Specimen_001_Sample_079_079.fcs'
##  red+: 6 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_080_080.fcs'
##  red+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_081_081.fcs'
##  red+: 7 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_082_082.fcs'
##  red+: 23 of 30216 events (0.08%)
## 
## filter summary for frame 'Specimen_001_Sample_083_083.fcs'
##  red+: 13 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_084_084.fcs'
##  red+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_085_085.fcs'
##  red+: 11 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_086_086.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_087_087.fcs'
##  red+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_088_088.fcs'
##  red+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_089_089.fcs'
##  red+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_090_090.fcs'
##  red+: 48 of 30216 events (0.16%)
## 
## filter summary for frame 'Specimen_001_Sample_091_091.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_092_092.fcs'
##  red+: 2 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_093_093.fcs'
##  red+: 788 of 30216 events (2.61%)
## 
## filter summary for frame 'Specimen_001_Sample_094_094.fcs'
##  red+: 214 of 30216 events (0.71%)
## 
## filter summary for frame 'Specimen_001_Sample_095_095.fcs'
##  red+: 0 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_096_096.fcs'
##  red+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_097_097.fcs'
##  red+: 9 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_098_098.fcs'
##  red+: 506 of 30216 events (1.67%)
## 
## filter summary for frame 'Specimen_001_Sample_099_099.fcs'
##  red+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_100_100.fcs'
##  red+: 6 of 30216 events (0.02%)
percent_yellow = toTable(summary(result_yellow)) %>%
    mutate(x = 6e5, y = -5e2, colour="yellow", order=phenoData(events)$order) %>%
    rename(name=sample) %>%
    left_join(metadata, by="name")
## filter summary for frame 'Specimen_001_Sample_001_001.fcs'
##  yellow+: 13 of 30216 events (0.04%)
## 
## filter summary for frame 'Specimen_001_Sample_002_002.fcs'
##  yellow+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_003_003.fcs'
##  yellow+: 19 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_004_004.fcs'
##  yellow+: 25 of 30216 events (0.08%)
## 
## filter summary for frame 'Specimen_001_Sample_005_005.fcs'
##  yellow+: 6 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_006_006.fcs'
##  yellow+: 5 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_007_007.fcs'
##  yellow+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_008_008.fcs'
##  yellow+: 4 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_009_009.fcs'
##  yellow+: 7 of 30216 events (0.02%)
## 
## filter summary for frame 'Specimen_001_Sample_010_010.fcs'
##  yellow+: 26 of 30216 events (0.09%)
## 
## filter summary for frame 'Specimen_001_Sample_011_011.fcs'
##  yellow+: 18 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_012_012.fcs'
##  yellow+: 15 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_013_013.fcs'
##  yellow+: 10 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_014_014.fcs'
##  yellow+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_015_015.fcs'
##  yellow+: 10 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_016_016.fcs'
##  yellow+: 32 of 30216 events (0.11%)
## 
## filter summary for frame 'Specimen_001_Sample_017_017.fcs'
##  yellow+: 8 of 30216 events (0.03%)
## 
## filter summary for frame 'Specimen_001_Sample_018_018.fcs'
##  yellow+: 15 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_019_019.fcs'
##  yellow+: 1 of 30216 events (0.00%)
## 
## filter summary for frame 'Specimen_001_Sample_020_020.fcs'
##  yellow+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_021_021.fcs'
##  yellow+: 595 of 30216 events (1.97%)
## 
## filter summary for frame 'Specimen_001_Sample_022_022.fcs'
##  yellow+: 253 of 30216 events (0.84%)
## 
## filter summary for frame 'Specimen_001_Sample_023_023.fcs'
##  yellow+: 269 of 30216 events (0.89%)
## 
## filter summary for frame 'Specimen_001_Sample_024_024.fcs'
##  yellow+: 3667 of 30216 events (12.14%)
## 
## filter summary for frame 'Specimen_001_Sample_025_025.fcs'
##  yellow+: 735 of 30216 events (2.43%)
## 
## filter summary for frame 'Specimen_001_Sample_026_026.fcs'
##  yellow+: 668 of 30216 events (2.21%)
## 
## filter summary for frame 'Specimen_001_Sample_027_027.fcs'
##  yellow+: 5112 of 30216 events (16.92%)
## 
## filter summary for frame 'Specimen_001_Sample_028_028.fcs'
##  yellow+: 32 of 30216 events (0.11%)
## 
## filter summary for frame 'Specimen_001_Sample_029_029.fcs'
##  yellow+: 1454 of 30216 events (4.81%)
## 
## filter summary for frame 'Specimen_001_Sample_030_030.fcs'
##  yellow+: 2165 of 30216 events (7.17%)
## 
## filter summary for frame 'Specimen_001_Sample_031_031.fcs'
##  yellow+: 2266 of 30216 events (7.50%)
## 
## filter summary for frame 'Specimen_001_Sample_032_032.fcs'
##  yellow+: 470 of 30216 events (1.56%)
## 
## filter summary for frame 'Specimen_001_Sample_033_033.fcs'
##  yellow+: 6372 of 30216 events (21.09%)
## 
## filter summary for frame 'Specimen_001_Sample_034_034.fcs'
##  yellow+: 795 of 30216 events (2.63%)
## 
## filter summary for frame 'Specimen_001_Sample_035_035.fcs'
##  yellow+: 1644 of 30216 events (5.44%)
## 
## filter summary for frame 'Specimen_001_Sample_036_036.fcs'
##  yellow+: 511 of 30216 events (1.69%)
## 
## filter summary for frame 'Specimen_001_Sample_037_037.fcs'
##  yellow+: 2004 of 30216 events (6.63%)
## 
## filter summary for frame 'Specimen_001_Sample_038_038.fcs'
##  yellow+: 628 of 30216 events (2.08%)
## 
## filter summary for frame 'Specimen_001_Sample_039_039.fcs'
##  yellow+: 478 of 30216 events (1.58%)
## 
## filter summary for frame 'Specimen_001_Sample_040_040.fcs'
##  yellow+: 267 of 30216 events (0.88%)
## 
## filter summary for frame 'Specimen_001_Sample_041_041.fcs'
##  yellow+: 9167 of 30216 events (30.34%)
## 
## filter summary for frame 'Specimen_001_Sample_042_042.fcs'
##  yellow+: 17 of 30216 events (0.06%)
## 
## filter summary for frame 'Specimen_001_Sample_043_043.fcs'
##  yellow+: 74 of 30216 events (0.24%)
## 
## filter summary for frame 'Specimen_001_Sample_044_044.fcs'
##  yellow+: 4313 of 30216 events (14.27%)
## 
## filter summary for frame 'Specimen_001_Sample_045_045.fcs'
##  yellow+: 13027 of 30216 events (43.11%)
## 
## filter summary for frame 'Specimen_001_Sample_046_046.fcs'
##  yellow+: 6286 of 30216 events (20.80%)
## 
## filter summary for frame 'Specimen_001_Sample_047_047.fcs'
##  yellow+: 7697 of 30216 events (25.47%)
## 
## filter summary for frame 'Specimen_001_Sample_048_048.fcs'
##  yellow+: 5631 of 30216 events (18.64%)
## 
## filter summary for frame 'Specimen_001_Sample_049_049.fcs'
##  yellow+: 6542 of 30216 events (21.65%)
## 
## filter summary for frame 'Specimen_001_Sample_050_050.fcs'
##  yellow+: 4201 of 30216 events (13.90%)
## 
## filter summary for frame 'Specimen_001_Sample_051_051.fcs'
##  yellow+: 10390 of 30216 events (34.39%)
## 
## filter summary for frame 'Specimen_001_Sample_052_052.fcs'
##  yellow+: 162 of 30216 events (0.54%)
## 
## filter summary for frame 'Specimen_001_Sample_053_053.fcs'
##  yellow+: 1001 of 30216 events (3.31%)
## 
## filter summary for frame 'Specimen_001_Sample_054_054.fcs'
##  yellow+: 3516 of 30216 events (11.64%)
## 
## filter summary for frame 'Specimen_001_Sample_055_055.fcs'
##  yellow+: 39 of 30216 events (0.13%)
## 
## filter summary for frame 'Specimen_001_Sample_056_056.fcs'
##  yellow+: 10463 of 30216 events (34.63%)
## 
## filter summary for frame 'Specimen_001_Sample_057_057.fcs'
##  yellow+: 170 of 30216 events (0.56%)
## 
## filter summary for frame 'Specimen_001_Sample_058_058.fcs'
##  yellow+: 1786 of 30216 events (5.91%)
## 
## filter summary for frame 'Specimen_001_Sample_059_059.fcs'
##  yellow+: 32 of 30216 events (0.11%)
## 
## filter summary for frame 'Specimen_001_Sample_060_060.fcs'
##  yellow+: 470 of 30216 events (1.56%)
## 
## filter summary for frame 'Specimen_001_Sample_061_061.fcs'
##  yellow+: 7121 of 30216 events (23.57%)
## 
## filter summary for frame 'Specimen_001_Sample_062_062.fcs'
##  yellow+: 50 of 30216 events (0.17%)
## 
## filter summary for frame 'Specimen_001_Sample_063_063.fcs'
##  yellow+: 67 of 30216 events (0.22%)
## 
## filter summary for frame 'Specimen_001_Sample_064_064.fcs'
##  yellow+: 4316 of 30216 events (14.28%)
## 
## filter summary for frame 'Specimen_001_Sample_065_065.fcs'
##  yellow+: 4846 of 30216 events (16.04%)
## 
## filter summary for frame 'Specimen_001_Sample_066_066.fcs'
##  yellow+: 7299 of 30216 events (24.16%)
## 
## filter summary for frame 'Specimen_001_Sample_067_067.fcs'
##  yellow+: 5880 of 30216 events (19.46%)
## 
## filter summary for frame 'Specimen_001_Sample_068_068.fcs'
##  yellow+: 14198 of 30216 events (46.99%)
## 
## filter summary for frame 'Specimen_001_Sample_069_069.fcs'
##  yellow+: 78 of 30216 events (0.26%)
## 
## filter summary for frame 'Specimen_001_Sample_070_070.fcs'
##  yellow+: 7235 of 30216 events (23.94%)
## 
## filter summary for frame 'Specimen_001_Sample_071_071.fcs'
##  yellow+: 2859 of 30216 events (9.46%)
## 
## filter summary for frame 'Specimen_001_Sample_072_072.fcs'
##  yellow+: 55 of 30216 events (0.18%)
## 
## filter summary for frame 'Specimen_001_Sample_073_073.fcs'
##  yellow+: 3686 of 30216 events (12.20%)
## 
## filter summary for frame 'Specimen_001_Sample_074_074.fcs'
##  yellow+: 3660 of 30216 events (12.11%)
## 
## filter summary for frame 'Specimen_001_Sample_075_075.fcs'
##  yellow+: 66 of 30216 events (0.22%)
## 
## filter summary for frame 'Specimen_001_Sample_076_076.fcs'
##  yellow+: 9323 of 30216 events (30.85%)
## 
## filter summary for frame 'Specimen_001_Sample_077_077.fcs'
##  yellow+: 59 of 30216 events (0.20%)
## 
## filter summary for frame 'Specimen_001_Sample_078_078.fcs'
##  yellow+: 1820 of 30216 events (6.02%)
## 
## filter summary for frame 'Specimen_001_Sample_079_079.fcs'
##  yellow+: 14 of 30216 events (0.05%)
## 
## filter summary for frame 'Specimen_001_Sample_080_080.fcs'
##  yellow+: 48 of 30216 events (0.16%)
## 
## filter summary for frame 'Specimen_001_Sample_081_081.fcs'
##  yellow+: 3748 of 30216 events (12.40%)
## 
## filter summary for frame 'Specimen_001_Sample_082_082.fcs'
##  yellow+: 6588 of 30216 events (21.80%)
## 
## filter summary for frame 'Specimen_001_Sample_083_083.fcs'
##  yellow+: 38 of 30216 events (0.13%)
## 
## filter summary for frame 'Specimen_001_Sample_084_084.fcs'
##  yellow+: 361 of 30216 events (1.19%)
## 
## filter summary for frame 'Specimen_001_Sample_085_085.fcs'
##  yellow+: 1046 of 30216 events (3.46%)
## 
## filter summary for frame 'Specimen_001_Sample_086_086.fcs'
##  yellow+: 616 of 30216 events (2.04%)
## 
## filter summary for frame 'Specimen_001_Sample_087_087.fcs'
##  yellow+: 1742 of 30216 events (5.77%)
## 
## filter summary for frame 'Specimen_001_Sample_088_088.fcs'
##  yellow+: 263 of 30216 events (0.87%)
## 
## filter summary for frame 'Specimen_001_Sample_089_089.fcs'
##  yellow+: 23 of 30216 events (0.08%)
## 
## filter summary for frame 'Specimen_001_Sample_090_090.fcs'
##  yellow+: 1530 of 30216 events (5.06%)
## 
## filter summary for frame 'Specimen_001_Sample_091_091.fcs'
##  yellow+: 378 of 30216 events (1.25%)
## 
## filter summary for frame 'Specimen_001_Sample_092_092.fcs'
##  yellow+: 293 of 30216 events (0.97%)
## 
## filter summary for frame 'Specimen_001_Sample_093_093.fcs'
##  yellow+: 35 of 30216 events (0.12%)
## 
## filter summary for frame 'Specimen_001_Sample_094_094.fcs'
##  yellow+: 1392 of 30216 events (4.61%)
## 
## filter summary for frame 'Specimen_001_Sample_095_095.fcs'
##  yellow+: 3 of 30216 events (0.01%)
## 
## filter summary for frame 'Specimen_001_Sample_096_096.fcs'
##  yellow+: 1798 of 30216 events (5.95%)
## 
## filter summary for frame 'Specimen_001_Sample_097_097.fcs'
##  yellow+: 492 of 30216 events (1.63%)
## 
## filter summary for frame 'Specimen_001_Sample_098_098.fcs'
##  yellow+: 602 of 30216 events (1.99%)
## 
## filter summary for frame 'Specimen_001_Sample_099_099.fcs'
##  yellow+: 1043 of 30216 events (3.45%)
## 
## filter summary for frame 'Specimen_001_Sample_100_100.fcs'
##  yellow+: 49 of 30216 events (0.16%)
percentage = bind_rows(percent_red, percent_yellow) %>%
    group_by(name) %>% 
    mutate(total_yellow_mcherry = sum(true)) %>%
    mutate(fraction_fluor = round(true/total_yellow_mcherry, 3)) %>%
    mutate(percent_fluor = fraction_fluor*100) %>%
    mutate(label = paste(true, "events"))
ggplot() + 
    geom_point(data=events, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="black") +
    geom_point(data=events_yellow, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="goldenrod") +
    geom_point(data=events_red, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="red3") +
    geom_text(data=percentage, aes(x=x, y=y, label=label, colour=colour), size=3, hjust=1) +
    geom_text(data=percentage, aes(x=6e5, y=3e5, label=paste0(count, " events total")), colour="grey27", size=3, hjust=1) +
    scale_colour_manual(values=c("red3", "goldenrod4")) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 5e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 7e5)) +
    theme_linedraw(12) +
    theme(panel.grid = element_blank(), 
          legend.position = "none") +
    facet_grid(Mouse_Treatment~Day) 
## Warning: Removed 174 rows containing missing values (geom_point).

ggsave("figures/exp19_yellow_red_scatterplot.png", width=12, height=40, dpi=150)
## Warning: Removed 174 rows containing missing values (geom_point).

Make plots split on NT / GFPT

samples = metadata %>%
    filter(Treatment == "NT") %>%
    pull(FlowSampleNumber)

percentage_samples = percentage %>%
    filter(FlowSampleNumber %in% samples)

ggplot() + 
    geom_point(data=events[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="black") +
    geom_point(data=events_yellow[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="goldenrod") +
    geom_point(data=events_red[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="red3") +
    geom_text(data=percentage_samples, aes(x=x, y=y, label=label, colour=colour), size=3, hjust=1) +
    geom_text(data=percentage_samples, aes(x=6e5, y=3e5, label=paste0(count, " events total")), colour="grey27", size=3, hjust=1) +
    scale_colour_manual(values=c("red3", "goldenrod4")) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 5e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 7e5)) +
    theme_linedraw(12) +
    theme(panel.grid = element_blank(), legend.position = "none") +
    facet_grid(Mouse_Treatment~Day) 
## Warning: Removed 156 rows containing missing values (geom_point).

ggsave("figures/exp19_yellow_red_scatterplot_NT.png", width=12, height=20, dpi=150)
## Warning: Removed 156 rows containing missing values (geom_point).
samples = metadata %>%
    filter(Treatment == "GFPT") %>%
    pull(FlowSampleNumber)

percentage_samples = percentage %>%
    filter(FlowSampleNumber %in% samples)

ggplot() + 
    geom_point(data=events[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="black") +
    geom_point(data=events_yellow[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="goldenrod") +
    geom_point(data=events_red[samples], aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.5, alpha=0.2, colour="red3") +
    geom_text(data=percentage_samples, aes(x=x, y=y, label=label, colour=colour), size=3, hjust=1) +
    geom_text(data=percentage_samples, aes(x=6e5, y=3e5, label=paste0(count, " events total")), colour="grey27", size=3, hjust=1) +
    scale_colour_manual(values=c("red3", "goldenrod4")) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 5e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 7e5)) +
    theme_linedraw(12) +
    theme(panel.grid = element_blank(),
          legend.position = "none") +
    facet_grid(Mouse_Treatment~Day) 
## Warning: Removed 18 rows containing missing values (geom_point).

ggsave("figures/exp19_yellow_red_scatterplot_GFPT.png", width=12, height=20, dpi=150)
## Warning: Removed 18 rows containing missing values (geom_point).

Make plots for representative mice

samples=c(4, 24, 44, 64, 84, 13, 33, 53, 73, 93)
events_M4_M13 = events[samples]
events_red_M4_M13 = events_red[samples]
events_yellow_M4_M13 = events_yellow[samples]
percentage_M4_M13 = percentage %>%
    filter(Mouse == "Mouse 4" | Mouse == "Mouse 13") 
ggplot() + 
    geom_point(data=events_M4_M13, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.75, alpha=0.2, colour="black") +
    geom_point(data=events_yellow_M4_M13, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.75, alpha=0.2, colour="goldenrod") +
    geom_point(data=events_red_M4_M13, aes(x=`530/30 Blue B-A`, y=`610/20 YG C-A`), shape=16, size=0.75, alpha=0.2, colour="red3") +
    geom_text(data=percentage_M4_M13, aes(x=x, y=y, label=label, colour=colour), size=3, hjust=1) +
    geom_text(data=percentage_M4_M13, aes(x=6e5, y=3e5, label=paste0(count, " events total")), colour="grey27", size=3, hjust=1) +
    scale_colour_manual(values=c("red3", "goldenrod4")) +
    ggcyto::scale_y_flowJo_biexp(name="mCherry intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 5e5)) +
    ggcyto::scale_x_flowJo_biexp(name="GFP intensity", widthBasis=-500, pos=5, neg=0, breaks=c(0, 1e3,1e4,1e5), limits=c(-1.5e3, 7e5)) +
    theme_linedraw(14) +
    theme(panel.grid = element_blank(), 
          axis.title.x = element_text(colour="black"), 
          axis.title.y = element_text(colour="black"), 
          legend.position = "none", 
          panel.border=element_rect(size=1)) +
    facet_grid(Treatment_Mouse~Day)

ggsave("figures/exp19_yellow_red_scatterplot_M4_M13.png", width=12, height=5, dpi=300, device="png")

Calculate and subtract background

#find highest background yellow before E. coli introduced (Day -5)
bg_yellow_high = percentage %>%
    dplyr::filter(Timepoint == -5, population == "yellow+") %>%
    pull(true) %>%
    max()

#find highest background red for all NT samples at all timespoints
bg_red_high = percentage %>%
    dplyr::filter(Treatment=="NT", population == "red+") %>%
    pull(true) %>%
    max()

#set background for yellow (Day -5) and red (Day 0) based on each mouse
percentage = percentage %>%
    group_by(Mouse, population) %>%
    mutate(bg = case_when(population=="yellow+" ~ true[which(Timepoint == -5)], 
                          population=="red+" ~ true[which(Timepoint == 0)])) %>%
    ungroup() %>%
    mutate(positive_events_minusbg = true - bg)

#set all negative values or 0 values to one
percentage = percentage %>%
    mutate(positive_events_minusbg_nozeros = as.double(positive_events_minusbg)) %>%
    mutate(positive_events_minusbg_nozeros = case_when(positive_events_minusbg_nozeros <= 0 ~ 1, 
                                               positive_events_minusbg_nozeros > 0 ~ positive_events_minusbg_nozeros))
#remove Mouse 8 T=2 based on scatterplot
percentage = percentage %>% filter(!(Mouse == "Mouse 8" & Day == "Day 0"))

Plot events over time for individual mice

ggplot(percentage) +
    annotate("rect", xmin=0.25, xmax=7.25, ymin=0, ymax=Inf, fill="honeydew2", colour=NA, alpha=0.75) +
    geom_line(aes(x=Timepoint, y=positive_events_minusbg_nozeros, group=interaction(Mouse, population), colour=population), size=0.5) +
    geom_point(aes(x=Timepoint, y=positive_events_minusbg_nozeros, group=interaction(Mouse, population), colour=population), size=2) +
    scale_colour_manual(values=c("red3", "goldenrod3"), labels=c("mCherry+", "GFP+ mCherry+")) +
    scale_y_log10(name="Positive events / Total events", labels = trans_format("log10", math_format(10^.x))) +
    scale_x_continuous(name="Day", breaks=seq(-4,15,2)) +
    theme_linedraw(12) +
    theme(legend.position = "top", 
          panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank(), 
          panel.grid.minor.x=element_blank(), panel.grid.major.x = element_blank(), 
          panel.border=element_rect(size=1.5)) +
    annotation_logticks(sides="l") +
    facet_wrap(~Mouse_Treatment, nrow=4)
## Warning: Transformation introduced infinite values in continuous y-axis

ggsave("figures/exp19_timecourse_yellow_red_byMouse_unfiltered.png", width=12, height=8)
## Warning: Transformation introduced infinite values in continuous y-axis


#filter out timepoints where red AND yellow are both below the highest background observed for each multipled by a factor
percentage_filtered = percentage %>%
    mutate(bg_high = case_when(population == "yellow+" ~ bg_yellow_high, population == "red+" ~ bg_red_high)) %>%  #background depending on fluor
    mutate(below_background = ifelse(true > bg_high * 3, "no", "yes")) %>%                                         #mark whether above or below threshold
    mutate(Mouse_Timepoint = paste(Mouse, Timepoint))

#make new table of mouse+timepoint to filter by 
filter_below = percentage_filtered %>%
    dplyr::select(Mouse, Timepoint, Mouse_Timepoint, population, bg, below_background) %>%
    dplyr::filter(below_background == "yes") %>% 
    dplyr::filter(Timepoint != -5, Timepoint != 0) %>%
    pivot_wider(names_from=population, values_from=bg) %>%
    dplyr::filter(is.na(`yellow+`) == FALSE & is.na(`red+`) == FALSE)

`%notin%` <- Negate(`%in%`)

percentage_filtered = percentage_filtered %>%
    dplyr::filter(Mouse_Timepoint %notin% filter_below$Mouse_Timepoint)
ggplot(percentage_filtered) +
    annotate("rect", xmin=0.25, xmax=7.25, ymin=0, ymax=Inf, fill="honeydew2", colour=NA, alpha=0.75) +
    geom_line(aes(x=Timepoint, y=positive_events_minusbg_nozeros, group=interaction(Mouse, population), colour=population), size=0.5) +
    geom_point(aes(x=Timepoint, y=positive_events_minusbg_nozeros, group=interaction(Mouse, population), colour=population), size=2) +
    scale_colour_manual(values=c("red3", "goldenrod3"), labels=c("mCherry+", "GFP+ mCherry+")) +
    scale_y_log10(name="Positive events / Total events", labels = trans_format("log10", math_format(10^.x))) +
    scale_x_continuous(name="Day", breaks=seq(-4,15,2)) +
    theme_linedraw(12) +
    theme(legend.position = "top", 
          panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank(), 
          panel.grid.minor.x=element_blank(), panel.grid.major.x = element_blank(), 
          panel.border=element_rect(size=1.5)) +
    annotation_logticks(sides="l") +
    facet_wrap(~Mouse_Treatment, nrow=4)
## Warning: Transformation introduced infinite values in continuous y-axis

ggsave("figures/exp19_timecourse_yellow_red_byMouse.png", height=8, width=12, dpi=150)
## Warning: Transformation introduced infinite values in continuous y-axis
ggsave("figures/exp19_timecourse_yellow_red_byMouse.pdf", height=8, width=12)
## Warning: Transformation introduced infinite values in continuous y-axis


Plot percent yellow / red at Day 14

dayfourteen = percentage_filtered %>%
    dplyr::filter(Timepoint == 14) 
#calculate percent positive yellow/red using background subtracted-positive events (no negatives)
dayfourteen = dayfourteen %>%
    mutate(positive_events_minusbg_nonegs = as.double(positive_events_minusbg)) %>%
    mutate(positive_events_minusbg_nonegs = case_when(positive_events_minusbg_nonegs <= 0 ~ 0, 
                                                      positive_events_minusbg_nonegs > 0 ~ positive_events_minusbg_nonegs)) %>%
    group_by(Mouse, Timepoint) %>%
    mutate(positive_events_minusbg_nonegs_yellow_red_total = sum(positive_events_minusbg_nonegs)) %>%
    mutate(positive_events_minusbg_nonegs_percent_fluor = positive_events_minusbg_nonegs / positive_events_minusbg_nonegs_yellow_red_total * 100) %>%
    ungroup()
#order mice by red fluor
red_ranked = dayfourteen %>%
    filter(population=="red+") %>%
    arrange(rev(Treatment), positive_events_minusbg_nonegs_percent_fluor) %>%
    pull(Mouse)

dayfourteen$Mouse = factor(dayfourteen$Mouse, levels=red_ranked)
dayfourteen$population = factor(dayfourteen$population, levels=c("yellow+", "red+"))

#calculate background red of NT group to draw line
bg_red_nt = dayfourteen %>%
    dplyr::filter(Treatment=="NT" & population=="red+") %>%
    pull(positive_events_minusbg_nonegs_percent_fluor) %>%
    max()

#make df of labels to plot percentage red value on bar plot
labels = dayfourteen %>%
    dplyr::filter(population == "red+") %>%
    dplyr::filter(positive_events_minusbg_nonegs_percent_fluor > bg_red_nt) %>% 
    mutate(labels = paste0(round(positive_events_minusbg_nonegs_percent_fluor, 0), "%")) 
ggplot() +
     geom_bar(data=dayfourteen, aes(x=Mouse, y=positive_events_minusbg_nonegs_percent_fluor, fill=population), stat = "identity") +
     geom_text(data=labels, aes(x=Mouse, y=positive_events_minusbg_nonegs_percent_fluor-4.5, label=labels), colour="white", size=3) +
     scale_fill_manual(values = c("darkgoldenrod2","red3"), labels=c("mCherry+ GFP+", "mCherry+ only")) +
     geom_hline(yintercept = bg_red_nt, linetype="dashed", size=0.3) +
     scale_y_continuous(name="Day 14  Percent GFP+ mCherry+ and mCherry+ events\n(background subtracted)\n", 
                        expand = c(0, 0), position="right", breaks=c(0,25,50,75,100), labels=c(0,25,50,75,100)) +
     scale_x_discrete(name = "", position = "top") +
     coord_flip() +
     theme_linedraw(14) +
     theme(legend.position = "bottom", legend.title=element_blank(), panel.grid=element_blank(),
           plot.margin=unit(c(5, 8, 5, 15),"mm"), 
           panel.border = element_rect(size=1.25), 
           axis.title.x = element_text(size=12)) +
     guides(fill = guide_legend(nrow = 1))

ggsave("figures/exp19_percent_yellow_red_barplot.pdf", width=5.5, height=6)

Log session

sessionInfo()
## R version 3.6.2 (2019-12-12)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] forcats_0.5.1            stringr_1.4.0            dplyr_1.0.5             
##  [4] purrr_0.3.4              readr_1.4.0              tidyr_1.1.3             
##  [7] tibble_3.1.0             tidyverse_1.3.1          Phenoflow_1.1.2         
## [10] foreach_1.5.1            flowAI_1.16.0            flowFDA_0.99            
## [13] mclust_5.4.7             multcomp_1.4-16          TH.data_1.0-10          
## [16] MASS_7.3-53.1            survival_3.2-10          mvtnorm_1.1-1           
## [19] flowFP_1.44.0            flowViz_1.50.0           lattice_0.20-41         
## [22] flowClean_1.24.0         cowplot_1.1.1            scales_1.1.1            
## [25] ggcyto_1.12.0            flowWorkspace_3.32.0     ncdfFlow_2.30.1         
## [28] BH_1.75.0-0              RcppArmadillo_0.10.4.0.0 ggplot2_3.3.3           
## [31] flowCore_1.52.1         
## 
## loaded via a namespace (and not attached):
##   [1] readxl_1.3.1         changepoint_2.2.2    backports_1.2.1     
##   [4] plyr_1.8.6           igraph_1.2.6         splines_3.6.2       
##   [7] digest_0.6.27        htmltools_0.5.1.1    fansi_0.4.2         
##  [10] magrittr_2.0.1       cluster_2.1.1        sfsmisc_1.1-11      
##  [13] recipes_0.1.15       Biostrings_2.52.0    modelr_0.1.8        
##  [16] gower_0.2.2          RcppParallel_5.1.2   matrixStats_0.58.0  
##  [19] sandwich_3.0-0       prettyunits_1.1.1    jpeg_0.1-8.1        
##  [22] colorspace_2.0-0     rvest_1.0.0          rrcov_1.5-5         
##  [25] haven_2.4.0          xfun_0.22            crayon_1.4.1        
##  [28] jsonlite_1.7.2       hexbin_1.28.2        graph_1.62.0        
##  [31] zoo_1.8-9            iterators_1.0.13     ape_5.4-1           
##  [34] glue_1.4.2           gtable_0.3.0         ipred_0.9-11        
##  [37] zlibbioc_1.30.0      XVector_0.24.0       phyloseq_1.28.0     
##  [40] IDPmisc_1.1.20       Rgraphviz_2.28.0     Rhdf5lib_1.6.3      
##  [43] BiocGenerics_0.32.0  DEoptimR_1.0-8       DBI_1.1.1           
##  [46] Rcpp_1.0.6           progress_1.2.2       bit_4.0.4           
##  [49] stats4_3.6.2         lava_1.6.9           prodlim_2019.11.13  
##  [52] httr_1.4.2           RColorBrewer_1.1-2   ellipsis_0.3.1      
##  [55] farver_2.1.0         pkgconfig_2.0.3      nnet_7.3-15         
##  [58] sass_0.3.1           dbplyr_2.1.1         utf8_1.2.1          
##  [61] caret_6.0-86         labeling_0.4.2       tidyselect_1.1.0    
##  [64] rlang_0.4.10         reshape2_1.4.4       cellranger_1.1.0    
##  [67] munsell_0.5.0        tools_3.6.2          cli_2.4.0           
##  [70] generics_0.1.0       ade4_1.7-16          broom_0.7.6         
##  [73] evaluate_0.14        biomformat_1.12.0    yaml_2.2.1          
##  [76] fs_1.5.0             ModelMetrics_1.2.2.2 knitr_1.32          
##  [79] robustbase_0.93-7    nlme_3.1-152         xml2_1.3.2          
##  [82] rstudioapi_0.13      compiler_3.6.2       png_0.1-7           
##  [85] reprex_2.0.0         bslib_0.2.4          pcaPP_1.9-73        
##  [88] stringi_1.5.3        highr_0.8            Matrix_1.3-2        
##  [91] vegan_2.5-7          permute_0.9-5        multtest_2.40.0     
##  [94] vctrs_0.3.7          pillar_1.6.0         lifecycle_1.0.0     
##  [97] jquerylib_0.1.3      data.table_1.14.0    R6_2.5.0            
## [100] latticeExtra_0.6-29  KernSmooth_2.23-18   gridExtra_2.3       
## [103] IRanges_2.18.3       codetools_0.2-18     boot_1.3-27         
## [106] assertthat_0.2.1     rhdf5_2.28.1         withr_2.4.1         
## [109] S4Vectors_0.22.1     mgcv_1.8-34          parallel_3.6.2      
## [112] hms_1.0.0            grid_3.6.2           rpart_4.1-15        
## [115] timeDate_3043.102    class_7.3-18         rmarkdown_2.7       
## [118] pROC_1.17.0.1        Biobase_2.46.0       lubridate_1.7.10